using System;
using System.Net;
using System.Net.Sockets;
using QGameFramework;
using UnityEngine;

public class UDPClient : BaseComponent
{
    private UdpClient m_UdpClient;

    [SerializeField] private string ipAds;
    [SerializeField] private int port;

    private byte[] readBuff = new byte[1024];
    string recvStr = String.Empty;

    public void Send(string msg)
    {
        byte[] sendBytes = System.Text.Encoding.Default.GetBytes(msg);
        m_UdpClient.Send(sendBytes, sendBytes.Length, ipAds, port);
    }

    public void OnInit()
    {
        m_UdpClient = new UdpClient();
    }

    private void OnDestroy()
    {
        if (m_UdpClient != null)
        {
            m_UdpClient.Close();
            m_UdpClient = null;
        }
    }

    public void OnConnectedToServer(string ipAdr, int i)
    {
        this.ipAds = ipAdr;
        this.port = i;
    }
}